﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using HundredMilesSoftware.UltraID3Lib;

namespace HebrewID3FixTests
{
	public partial class FormMain : Form
	{
		DirectoryInfo m_oDirectory;
		IEnumerator<FileInfo> m_oCurrentFileEnumerator;
		FileInfo m_oCurrentFile;

		Encoding m_oCorrectEncoding = Encoding.GetEncoding(1255);

		public FormMain()
		{
			InitializeComponent();
			ColumnHeader l_oHeader = new ColumnHeader();
			l_oHeader.Text = "Type";
			listView1.Columns.Add(l_oHeader);

			for (int i = 0; i < 16; i++)
			{
				l_oHeader = new ColumnHeader();
				l_oHeader.Text = "Byte" + i.ToString("##");
				listView1.Columns.Add(l_oHeader);
			}

			PrintEncodingData();

			m_oCurrentFile = new FileInfo("C:\\Users\\Nitay\\Music\\Mp3s\\To Ipod\\הבילויים\\שכול וכישלון\\05 תנשב הרוח.mp3");
			FixHebrewFile();

		}

		private void PrintInfo(string a_sString)
		{
			textBoxInfo.Text += a_sString + "\r\n";
		}

		private void PrintEncodingData()
		{
			// Print the header.

			// For every encoding, compare the name properties with EncodingInfo.Name.
			// Display only the encodings that have one or more different names.
			foreach (EncodingInfo ei in Encoding.GetEncodings())
			{
				Encoding e = ei.GetEncoding();

				//				if ((ei.Name != e.BodyName) || (ei.Name != e.HeaderName) || (ei.Name != e.WebName))
				{
					PrintInfo("Name: " + ei.Name);
					PrintInfo("CodePage: " + ei.CodePage);
					PrintInfo("BodyName: " + e.BodyName);
					PrintInfo("HeaderName: " + e.HeaderName);
					PrintInfo("WebName: " + e.WebName);
					PrintInfo("Encoding.EncodingName:" + e.EncodingName);

					PrintInfo("");
				}

			}
		}

		private void buttonBrowse_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog l_oDialog = new FolderBrowserDialog();
			l_oDialog.SelectedPath = textBoxPath.Text;

			if (l_oDialog.ShowDialog() == DialogResult.OK)
			{
				textBoxPath.Text = l_oDialog.SelectedPath;
			}
		}

		private void buttonGo_Click(object sender, EventArgs e)
		{
			string l_sPath = textBoxPath.Text;

			m_oDirectory = new DirectoryInfo(l_sPath);
			FileInfo[] l_oFiles = m_oDirectory.GetFiles("*.mp3");
			m_oCurrentFileEnumerator = l_oFiles.ToList().GetEnumerator();

			/*            foreach (FileInfo l_oFile in l_oDir.GetFiles("*.mp3"))
						{
							UltraID3 l_oID3Tag = new UltraID3();

							l_oID3Tag.Read(l_oFile.FullName);

							textBox1.Text += l_oID3Tag.Artist + " - " + l_oID3Tag.Album + ": " + l_oID3Tag.Title + "\r\n";

							FixHebrew(l_oID3Tag);
						}*/
		}

		private void AddBytesToListView(string a_sName, byte[] a_abBytes)
		{
			ListViewItem l_oItem = new ListViewItem();

			l_oItem.Text = a_sName;

			foreach (byte l_bByte in a_abBytes)
				l_oItem.SubItems.Add(l_bByte.ToString("X2"));

			listView1.Items.Add(l_oItem);
		}

		private string FixHebrewString(string a_sString)
		{
			byte[] l_oUnicode = Encoding.Unicode.GetBytes(a_sString);
			byte[] l_oString = new byte[l_oUnicode.Length / 2];

			// Remove every other byte
			for (int i = 0; i < l_oUnicode.Length; i += 2)

				l_oString[i / 2] = l_oUnicode[i];


			return m_oCorrectEncoding.GetString(l_oString);
		}

		private void FixHebrewFile()
		{
			UltraID3 l_oID3Tag = new UltraID3();
			l_oID3Tag.Read(m_oCurrentFile.FullName);

			string l_sString = l_oID3Tag.Title;
			string l_sStringGood = "הבילויים";


			byte[] l_oUnicode = Encoding.Unicode.GetBytes(l_sString);
			//byte[] l_oASCII = Encoding.ASCII.GetBytes(l_sArtist);
			byte[] l_oASCII = m_oCorrectEncoding.GetBytes(l_sString);
			byte[] l_oUTF7 = Encoding.UTF7.GetBytes(l_sString);
			byte[] l_oUTF8 = Encoding.UTF8.GetBytes(l_sString);
			byte[] l_oUnicodeGood = Encoding.Unicode.GetBytes(l_sStringGood);
			//byte[] l_oAsciiGood = Encoding.ASCII.GetBytes(l_sStringGood);
			byte[] l_oAsciiGood = m_oCorrectEncoding.GetBytes(l_sStringGood);
			byte[] l_oUTF7Good = Encoding.UTF7.GetBytes(l_sStringGood);
			byte[] l_oUTF8Good = Encoding.UTF8.GetBytes(l_sStringGood);

			listView1.Items.Clear();
			AddBytesToListView("Unicode", l_oUnicode);
			AddBytesToListView("Unicode Good", l_oUnicodeGood);
			AddBytesToListView("ASCII", l_oASCII);
			AddBytesToListView("ASCII Good", l_oAsciiGood);
			AddBytesToListView("UTF7", l_oUTF7);
			AddBytesToListView("UTF7 Good", l_oUTF7Good);
			AddBytesToListView("UTF8", l_oUTF8);
			AddBytesToListView("UTF8 Good", l_oUTF8Good);

			listView1.AutoArrange = true;
			textBox1.Text += l_oID3Tag.Artist + " - " + l_oID3Tag.Album + ": " + l_oID3Tag.Title + "\r\n";
			textBox1.Text += FixHebrewString(l_oID3Tag.Artist) + " - " + FixHebrewString(l_oID3Tag.Album) + ": " + FixHebrewString(l_oID3Tag.Title) + "\r\n";

		}

		private void buttonNext_Click(object sender, EventArgs e)
		{
			m_oCurrentFileEnumerator.MoveNext();
			m_oCurrentFile = m_oCurrentFileEnumerator.Current;
			FixHebrewFile();
		}
	}
}
